import os
import socket
from datetime import datetime, timezone
from pathlib import Path

from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def utc_int_to_date(utc_int_time: int) -> datetime:
    """
    将UTC整数时间转换为日期。
    假设整数时间是Unix时间戳（秒）。
    """
    # 直接使用datetime.fromtimestamp将UTC时间转换为本地时间
    local_time = datetime.fromtimestamp(utc_int_time)
    # 如果需要转换为UTC时间，可以使用timezone.utc
    utc_time = datetime.fromtimestamp(utc_int_time, tz=timezone.utc)
    return utc_time


def condition(**kwargs):
    _dict = {}
    for key, value in kwargs.items():
        if value is not None and value != '':
            _dict[key] = value
    return _dict


def ray_condition(**kwargs):
    _list = []
    for key, value in kwargs.items():
        if value is not None and value != '':
            _list.append((key, "=", value))
    return _list


def get_local_ip():
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    return ip


def get_project_path():
    if hasattr(settings, 'BASE_DIR'):
        return settings.BASE_DIR
    if '__file__' in globals():
        return os.path.dirname(os.path.abspath(__file__))
    if '__file__' in locals():
        return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    return Path(__file__).resolve().parent.parent

